package cn.iocoder.yudao.module.contract.service.basicData;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.contract.dal.dataobject.client.ClientDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.frameworkAgreement.FrameworkAgreementDO;
import cn.iocoder.yudao.module.contract.dal.mysql.client.ClientMapper;
import cn.iocoder.yudao.module.contract.dal.mysql.company.CompanyMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicBankDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicBankMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.contract.enums.ErrorCodeConstants.*;

/**
 * 银行信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BasicBankServiceImpl extends ServiceImpl<BasicBankMapper, BasicBankDO> implements BasicBankService {

    @Resource
    private BasicBankMapper basicBankMapper;
    @Resource
    private CompanyMapper companyMapper;

    @Override
    public Long createBasicBank(BasicBankSaveReqVO createReqVO) {
        // 插入
        BasicBankDO basicBank = BeanUtils.toBean(createReqVO, BasicBankDO.class);
        validateUnique(basicBank);
        CompanyDO companyDO = companyMapper.selectOne(new LambdaQueryWrapper<CompanyDO>().eq(CompanyDO::getCompanyCode,basicBank.getCompanyCode()),false);
        if(companyDO==null){
            throw exception("所属公司不存在");
        }
        basicBank.setCompanyName(companyDO.getCompanyName());
        basicBankMapper.insert(basicBank);
        // 返回
        return basicBank.getId();
    }

    @Override
    public void updateBasicBank(BasicBankSaveReqVO updateReqVO) {
        // 校验存在
        validateBasicBankExists(updateReqVO.getId());
        // 更新
        BasicBankDO updateObj = BeanUtils.toBean(updateReqVO, BasicBankDO.class);
        validateUnique(updateObj);
        CompanyDO companyDO = companyMapper.selectOne(new LambdaQueryWrapper<CompanyDO>().eq(CompanyDO::getCompanyCode,updateReqVO.getCompanyCode()),false);
        if(companyDO==null){
            throw exception("所属公司不存在");
        }
        updateObj.setCompanyName(companyDO.getCompanyName());
        basicBankMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicBank(Long id) {
        // 校验存在
        validateBasicBankExists(id);
        // 删除
        basicBankMapper.deleteById(id);
    }

    private void validateBasicBankExists(Long id) {
        if (basicBankMapper.selectById(id) == null) {
            throw exception(BASIC_BANK_NOT_EXISTS);
        }
    }
    @VisibleForTesting
    void validateUnique(BasicBankDO basicBank) {
        long count = basicBankMapper.selectCount(new LambdaQueryWrapper<BasicBankDO>().eq(BasicBankDO::getBankAccount,basicBank.getBankAccount()).ne(basicBank.getId()!=null,BasicBankDO::getId,basicBank.getId()));
        // 如果 id 为空，说明不用比较是否为相同 id 的用户
        if (count>0 ) {
            throw exception("银行账号已存在");
        }
    }

    @Override
    public BasicBankDO getBasicBank(Long id) {
        return basicBankMapper.selectById(id);
    }

    @Override
    public PageResult<BasicBankDO> getBasicBankPage(BasicBankPageReqVO pageReqVO) {
        return basicBankMapper.selectPage(pageReqVO);
    }

}